<?php

namespace addons\booking\model;

use think\Model;
use fast\Tree;

class HouseFloor extends Model
{

    // 表名
    protected $name = 'house_floor';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [];

    //按楼栋楼层房间列表
    public static function get_room_floor($store_id = 0)
    {
        $list = self::getCategoryTree($store_id);
        $data = [];
        foreach ($list as $k=>$v){
            foreach ($v['children'] as $key=>$value){
                $value['name'] = $v['name'].$value['name'];
                $value['house_list'] = [];
                $data[] = $value;
            }
        }
        return $data ? $data : [];
    }

    // 获取分类树
    public static function getCategoryTree($store_id = 0)
    {
        $categories = collection(self::where('store_id',$store_id)->where('status',1)->field('id,pid,name')->select())->toArray();
        $tree = self::buildTree($categories);
        return $tree;
    }

    // 构建分类树
    private static function buildTree($categories, $parentId = 0)
    {
        $tree = array();
        foreach ($categories as $category) {
            if ($category['pid'] == $parentId) {
                $children = self::buildTree($categories, $category['id']);
                if (!empty($children)) {
                    $category['children'] = $children;
                }
                $tree[] = $category;
            }
        }
        return $tree;
    }

}
